<?php
namespace app\index\controller;

use think\Controller;
use app\index\controller\MysqlPdo;

class Index extends Controller
{
    public function index()
    {
        $curl = curl_init('https://www.17k.com/all');
        curl_setopt($curl, CURLOPT_FAILONERROR, true);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($curl);
        $preg = '#<tr class="bg0">.*<td class="td1">.*</td>.*<td class="td2">.*<a .*>(.*)</a>.*</td>.*<td class="td3">.*<span>.*<a class="jt" .*>(.*)</a>.*<td class="td4">.*<a .*>(.*)</a>.*</td>.*<td class="td5">(.*)</td>.*<td class="td6"><a .*>(.*)</a>.*</td>.*<td class="td7">(.*)</td>.*<td class="td8">.*<em class="fc2">.*</tr>#isU';
        preg_match_all($preg,$result,$arr);
        foreach($arr[1] as $key => $value){
            $data['type'] = $arr[1][$key];
            $data['book'] = $arr[2][$key];
            $data['title'] = $arr[3][$key];
            $data['number'] = $arr[4][$key];
            $data['name'] = $arr[5][$key];
            $data['time'] = $arr[6][$key];
            $db = new MysqlPdo();
            $res = $db->getAdd('books',$data);
        }

    }

    //展示搜索
    public function show()
    {
        $arr = [];
        if(isset($_GET['book'])&&$_GET['book']!=''){
            $arr['book'] = $_GET['book'];
        }
        $page = isset($_GET['page'])?$_GET['page']:1;
        $db = new MysqlPdo();
        $res = $db->getShow('books',$arr);
        echo $res;
    }

    // 删除
    public function del()
    {
        $id['id'] = $_POST['id'];
        $db = new MysqlPdo();
        $res = $db->getDel('books',$id);
        echo $res;
    }

    public function upd()
    {
        $value = $_POST['value'];
        $data['id'] = $_POST['data'];
        $field = $_POST['field'];
        $where[$field] =$value;
        $db = new MysqlPdo();
        $res = $db->getUpdate('books',$where,$data);
        echo $res;
    }
}
